package com.struts.annotation;

import com.struts.ActionForward;
import com.struts.ActionMapping;
import com.struts.util.FindClass;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: wangchen
 * Date: 16-5-8
 * Time: 上午10:21
 * To change this template use File | Settings | File Templates.
 */
public class AnnotationParse {
    public static Map<String, ActionMapping> parse(List<Class> list) {
        if (list == null || list.size() <= 0) {
            return null;
        }
        Map actionMap = new HashMap();
        for (Class clazz : list) {
            parseClassNode(clazz,actionMap);
        }
        return actionMap;
    }

    public static void parseClassNode(Class clazz, Map actionMap) {
        System.out.println("clazz::::"+clazz);
        System.out.println("clazz.toString::::"+clazz.toString());
        NameSpace nameSpace = (NameSpace) clazz.getAnnotation(NameSpace.class);
        if(nameSpace!=null){
            System.out.println("nameSpace:::"+nameSpace);
            System.out.println(nameSpace.value());
        }


        ActionMapping bean = nameSpaceExec(nameSpace,actionMap);
        bean.setType(clazz.getName());
        FormBean formBean = (FormBean) clazz.getAnnotation(FormBean.class);
        if(formBean!=null){
            System.out.println("formBean:::"+formBean);
            System.out.println(formBean.value());
        }


        formBeanExec(bean,formBean);


        Result result = (Result) clazz.getAnnotation(Result.class);
        if(result!=null){
            System.out.println("result:::"+result);
            System.out.println(result.value());
        }

        resultExec(bean,result);
       /* Method[] methods = clazz.getDeclaredMethods();
        for (Method method : methods){
            parseMethod(method,actionMap);
        }*/
    }

   /* public  static  void parseMethod(Method method,Map actionMap){
        Action action =  method.getAnnotation(Action.class);
        System.out.println("action::"+action);
        System.out.println("action.value::"+action.value());
    }*/

    public static void formBeanExec(ActionMapping bean,FormBean formBean){
        if(formBean!=null){
            bean.setFormBeanType(formBean.value());
        }
    }

    public static void resultExec(ActionMapping bean,Result result){
        if(result==null){
            return;
        }
        Map<String, ActionForward> forwards = bean.getActionForwards();
        String[] strs = result.value();
        String [] tem = null;
        ActionForward forward = null;
        for(String str : strs){
             tem = str.split("@");
            forward = new ActionForward();
            forward.setName(tem[0]);
            forward.setPath(tem[1]);
            forwards.put(forward.getName(),forward);
        }
    }

    public static ActionMapping nameSpaceExec(NameSpace nameSpace,Map actionMap){
        ActionMapping bean = new ActionMapping();
        if(nameSpace!=null){
            bean.setPath(nameSpace.value());
            actionMap.put(nameSpace.value(),bean);
        }

        return bean;
    }

    public static void main(String args []){
        //String basePackage= "com.bus.net2.NetsAction2";
        String basePackage= "com.bus.net2";
       // List<String> list = new ArrayList();
       // list.add(basePackage);
        List<Class> list = null;
        try {
            list = FindClass.getClassesByPackageName(basePackage);
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (ClassNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        System.out.println(list);
        parse(list);
    }

}
